package com.gaogzhen.aqs;

import lombok.extern.slf4j.Slf4j;

import java.util.Arrays;
import java.util.Random;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * @author Administrator
 */
@Slf4j(topic = "c.TestCountDownLatch01")
public class TestCountDownLatch01 {
    public static void main(String[] args) throws InterruptedException {
        CountDownLatch latch = new CountDownLatch(3);
        ExecutorService pool = Executors.newFixedThreadPool(10);
        Random random = new Random();
        String[] players = new String[10];
        for (String str: players) {
            str = "0%";
        }
        for (int i = 0; i < 10; i++) {
            int k = i;
            pool.submit(() -> {
                for (int j = 1; j <= 100; j++) {
                    try {
                        TimeUnit.MILLISECONDS.sleep(random.nextInt(100));
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    players[k] = j + "%";
                    System.out.print("\r" + Arrays.toString(players));
                }
                latch.countDown();
            });
        }

        latch.await();
        System.out.println("\n游戏开始\n");
        pool.shutdown();

    }
}
